﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using FMSpaceShooter.Screens;

namespace FMSpaceShooter.GameObjects
{
    sealed class Javelin : BulletBase
    {
        private ObjectBase enemy;
        public const string imageName = "Images/Bullets/javelin";
        private TimeSpan lockTime;
        private TimeSpan fireTime;
        bool doTarget = false;
        private float lockRotation = 0;
        bool doFire = false;
        public override int Damage { get { return 1000; } }
        public override bool DoesCollide { get { return false; } }


        public Javelin(GameplayScreen screen, ObjectBase enemy, Vector2 position, TimeSpan lockTime, TimeSpan fireTime)
            : base(screen, imageName, new Vector2(10,12))
        {
            this.position = position;
            this.lockTime = lockTime;
            this.enemy = enemy;
            this.fireTime = fireTime;
        }


        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            if (!screen.GOM.GameObjects.Contains(enemy))
            {
                this.Hit(0);
                return;
            }

            if (!doTarget)
            {
                if (gameTime.TotalGameTime > lockTime)
                {
                    doTarget = true;
                    position = screen.GOM.PlayerOne.Position;
                    rotation = (float)( screen.Random.NextDouble() * 7);
                }
            }
            else
            {
                if (!doFire)
                {
                    Vector2 direction = Util.Tools.AngleToVector(rotation);
                    float time = (float)(gameTime.TotalGameTime - lockTime).TotalMilliseconds;
                    float timeSpent = (float)(gameTime.TotalGameTime).TotalMilliseconds;
                    float timeRem = (float)(fireTime - gameTime.TotalGameTime).TotalMilliseconds;
                    float totalTime = (float)(fireTime - lockTime).TotalMilliseconds;
                    position += direction * (float)(gameTime.ElapsedGameTime.TotalMilliseconds * (timeRem / totalTime) * (200/totalTime));
                }
            }

            if (!doFire)
            {
                if (gameTime.TotalGameTime > fireTime)
                {
                    doFire = true;
                }
            }
            else
            {
                Vector2 direction = enemy.Position - position;
                direction.Normalize();

                var destAngle = Util.Tools.VectorToAngle(direction);

                rotation %= (float)(Math.PI * 2);

                var diff = MathHelper.WrapAngle(rotation - destAngle);

                if (diff > 0)
                {
                    rotation -= (float)(gameTime.ElapsedGameTime.TotalMilliseconds * 0.0005f * Math.Pow((gameTime.TotalGameTime - fireTime).TotalSeconds * 1000, 2) / 1000);
                }
                else if (diff < 0)
                {
                    rotation += (float)(gameTime.ElapsedGameTime.TotalMilliseconds * 0.0005f * Math.Pow((gameTime.TotalGameTime - fireTime).TotalSeconds * 1000, 2) / 1000);
                }

                position += Util.Tools.AngleToVector(rotation) * (float)(gameTime.ElapsedGameTime.TotalMilliseconds * (gameTime.TotalGameTime - fireTime).TotalSeconds* 15);


                //velocity += direction * (float)(gameTime.ElapsedGameTime.TotalMilliseconds * 100);
                //position += direction *  (float)((gameTime.TotalGameTime - (TimeSpan)start).TotalMilliseconds/1000);
                if (GameData.Collider.IsCollided(this, enemy, gameTime))
                {
                    enemy.Hit(this.Damage);
                    this.Hit(enemy.Damage);
                }
            }

            //velocity *= (float)(Math.Pow(0.5f,gameTime.ElapsedGameTime.TotalSeconds) * Math.Pow(0.9f, (gameTime.TotalGameTime - (TimeSpan)start).TotalMilliseconds/10000));
            //position += velocity * (float)gameTime.ElapsedGameTime.TotalSeconds;
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = screen.ScreenManager.SpriteBatch;
            if (doTarget)
            {
                
                float lockScale = (float)(0.5 + Math.Sin(lockRotation) * 0.1);
                spriteBatch.Draw(screen.GOM.GetTexture("Images/target"), enemy.Position,
                    null, (doFire ? Color.Red : Color.Yellow), lockRotation, Util.Tools.GetTextureVector(screen.GOM.GetTexture("Images/target")) / 2,
                    lockScale, SpriteEffects.None, 0f);

                lockRotation += (float)(gameTime.ElapsedGameTime.TotalSeconds * 2);
                lockRotation %= (float)(2 * Math.PI);
                //rotation = Util.Tools.VectorToAngle(velocity);

                base.Draw(gameTime);
            }
        }




    }
}
